package com.zp.base.controller.examMaintenance;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.base.api.vo.ExamTypeVo;
import com.zp.base.entity.examMaintenance.bo.ExamTypeBo;
import com.zp.base.service.examMaintenance.IExamTypeService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 检查项目类型exam_type控制器
 * 前端访问路由地址为:/base/examType
 *
 * @author zhang peng
 * @ date 2023-05-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/examType")
public class ExamTypeController extends BaseController {

    private final IExamTypeService iExamTypeService;

    /**
     * 查询检查项目类型exam_type列表 (分页)
     */
    @SaCheckPermission("base:examType:list")
    @GetMapping("/selectPage")
    public TableDataInfo<ExamTypeVo> selectPage (ExamTypeBo bo, PageQuery pageQuery) {
        return iExamTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询检查项目类型exam_type列表 (不分页)
     */
    @SaCheckPermission("base:examType:list")
    @GetMapping("/list")
    public R<List<ExamTypeVo>> list (ExamTypeBo bo) {
        List<ExamTypeVo> list = iExamTypeService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出检查项目类型exam_type列表
     */
    @SaCheckPermission("base:examType:export")
    @Log(title = "检查项目类型exam_type", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (ExamTypeBo bo, HttpServletResponse response) {
        List<ExamTypeVo> list = iExamTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "检查项目类型exam_type", ExamTypeVo.class, response);
    }

    /**
     * 获取检查项目类型exam_type详细信息
     *
     * @param examTypeId 主键
     */
    @SaCheckPermission("base:examType:list")
    @GetMapping("/{examTypeId}")
    public R<ExamTypeVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String examTypeId) {
        return R.ok(iExamTypeService.queryById(examTypeId));
    }

    /**
     * 新增检查项目类型exam_type
     */
    @SaCheckPermission("base:examType:add")
    @Log(title = "检查项目类型exam_type", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody ExamTypeBo bo) {
        return toAjax(iExamTypeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改检查项目类型exam_type
     */
    @SaCheckPermission("base:examType:edit")
    @Log(title = "检查项目类型exam_type", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@RequestBody ExamTypeBo bo) {
        return toAjax(iExamTypeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除检查项目类型exam_type
     *
     * @param examTypeIds 主键串
     */
    @SaCheckPermission("base:examType:remove")
    @Log(title = "检查项目类型exam_type", businessType = BusinessType.DELETE)
    @DeleteMapping("/{examTypeIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] examTypeIds) {
        return toAjax(iExamTypeService.deleteWithValidByIds(Arrays.asList(examTypeIds), true) ? 1 : 0);
    }

    @GetMapping("/refreshCacheExamType")
    public R<String> refreshCacheExamType () {
        iExamTypeService.refreshCache();
        return R.ok("刷新成功");
    }
}
